package com.atjyb.processschudulesimulator.Utils;

import com.atjyb.processschudulesimulator.pojo.Commend;
import com.atjyb.processschudulesimulator.pojo.Process;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class TXTReaderUtil {
    /**
     * 文件处理工具类
     *
     * @param txt_path txt文件路径
     * @return 返回process对象集合
     */
    public static List<Process> get(String txt_path) {

        /**
         * 返回的集合
         */
        List<Process> data = new ArrayList<>();

        /**
         * 进程id（唯一）
         * 自增，用来创建进程对象
         */
        int id_process = 1;

        /**
         * 临时存放，动态增加一个进程的指令
         * 后面再转成数组，因为数组不能改变容量
         */
        ArrayList<String> commendsList = null;

        /**
         * 进程对象--多个
         */
        Process process = null;

        /**
         * 操作数和操作符分开的集合
         */
        ArrayList<Commend> separateCommends=null;

        /**
         * 上面的commend
         */
        Commend commend = null;

        /**
         * 指令条数
         */
        int size=0;

        try (
                FileInputStream fis = new FileInputStream(txt_path);
                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

        ) {

            String line = "";
            line=br.readLine();
            while (line!= null) {

                if (RegexUtil.match(line)) {
                    separateCommends=new ArrayList<>();
                    process = new Process();
                    commendsList = new ArrayList<>();
                    process.setpID(id_process++);
                    process.setpName(line);
                    line = br.readLine();
                }

                for (int i = 0; !(RegexUtil.match(line)) && commendsList!=null; i++) {

                    commendsList.add(line);
                    line = br.readLine();
                    if (line==null) {
                        String[] commends = commendsList.toArray(new String[commendsList.size()]);
                        for (String c : commends) {
                            String operator=c.substring(0,1);
                            int number =Integer.valueOf(c.substring(1,3));
                            commend = new Commend(operator, number,number);
                            separateCommends.add(commend);
                        }
                        size=commends.length;
                        process.setCommendSize(size);
                        process.setCommends(commends);
                        process.setSeparateCommends(separateCommends);
                        data.add(process);
                        commendsList = null;
                        commend=null;
                        separateCommends=null;
                        process = null;
                        size=0;
                    }
                }

                if (commendsList!=null){
                    String[] commends = commendsList.toArray(new String[commendsList.size()]);
                    for (String c : commends) {
                        String operator=c.substring(0,1);
                        int number =Integer.valueOf(c.substring(1,3));
                        commend = new Commend(operator, number,number);
                        separateCommends.add(commend);
                    }
                    size=commends.length;
                    process.setCommendSize(size);
                    process.setCommends(commends);
                    process.setSeparateCommends(separateCommends);
                    data.add(process);
                    commendsList = null;
                    commend=null;
                    separateCommends=null;
                    process = null;
                    size=0;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        return data;
    }

}
